package org.example.frame;
import javax.swing.*;
import java.awt.event.*;
import org.example.operation.*;
import org.example.model.*;

/**
 * @author lenovo
 */

public class FirstFrame extends JFrame {
    public FirstFrame(){
        //组件准备
        JPanel panel = new JPanel();
        JTextField username = new JTextField(15);
        JTextField password = new JTextField(15);
        JButton login = new JButton("登录");
        JButton register = new JButton("注册");
        Box usernameBox = Box.createHorizontalBox();
        Box passwordBox = Box.createHorizontalBox();
        Box buttonBox = Box.createHorizontalBox();
        Box box = Box.createVerticalBox();
        //注册监视器
        login.addActionListener((ActionEvent e)->{
            User user = new User();
            if(username.getText().isEmpty() || password.getText().isEmpty()){
                JOptionPane.showMessageDialog(null,
                        "用户名或密码不能为空", null, JOptionPane.WARNING_MESSAGE);
            }else if(username.getText().matches(".*\\s+.*") ||
                    password.getText().matches(".*\\s+.*")){
                JOptionPane.showMessageDialog(null,
                        "用户名或密码不能有空格", null, JOptionPane.WARNING_MESSAGE);
            }else if(UserOperation.isNotLogin(username.getText(), password.getText())){
                JOptionPane.showMessageDialog(null,
                        "用户名或密码错误", null, JOptionPane.WARNING_MESSAGE);
            }else{
                JOptionPane.showMessageDialog(null,
                        "登录成功", null, JOptionPane.WARNING_MESSAGE);
                user.setUsername(username.getText());
                user.setPassword(password.getText());
                new SecondFrame(user);
                dispose();
            }
        });
        register.addActionListener((ActionEvent e)->{
            User user = new User();
            if(username.getText().isEmpty() || password.getText().isEmpty()){
                JOptionPane.showMessageDialog(null,
                        "用户名或密码不能为空", null, JOptionPane.WARNING_MESSAGE);
            }else if(username.getText().matches(".*\\s+.*") ||
                    password.getText().matches(".*\\s+.*")){
                JOptionPane.showMessageDialog(null,
                        "用户名或密码不能有空格", null, JOptionPane.WARNING_MESSAGE);
            }else if(UserOperation.isNotResister(username.getText())){
                JOptionPane.showMessageDialog(null,
                        "用户名已存在", null, JOptionPane.WARNING_MESSAGE);
            }else{
                JOptionPane.showMessageDialog(null,
                        "注册成功", null, JOptionPane.WARNING_MESSAGE);
                UserOperation.addUser(username.getText(),password.getText());
                user.setUsername(username.getText());
                user.setPassword(password.getText());
                new SecondFrame(user);
                dispose();
            }
        });
        //添加组件
        usernameBox.add(new JLabel("用户名： "));
        usernameBox.add(username);
        passwordBox.add(new JLabel("密码： "));
        passwordBox.add(password);
        buttonBox.add(login);
        buttonBox.add(Box.createHorizontalStrut(50));
        buttonBox.add(register);
        box.add(usernameBox);
        box.add(Box.createVerticalStrut(5));
        box.add(passwordBox);
        box.add(Box.createVerticalStrut(5));
        box.add(buttonBox);
        panel.add(box);
        add(panel);
        //窗口设置
        setTitle("学生成绩管理->登录与注册");
        setSize(500,400);
        setLocationRelativeTo(null);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        validate();
    }
}
